package com.relytosoft.mizar.media.camera.services.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.relytosoft.dubhe.common.core.base.page.PageQuery;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.dubhe.common.core.utils.BeanCopyUtils;
import com.relytosoft.dubhe.common.core.utils.CommonUtils;
import com.relytosoft.dubhe.common.log.annotation.OpLog;
import com.relytosoft.dubhe.common.log.enums.OpTypeEnum;
import com.relytosoft.mizar.media.camera.api.entity.CameraBase;
import com.relytosoft.mizar.media.camera.api.pojo.dto.CameraBaseDto;
import com.relytosoft.mizar.media.camera.api.pojo.vo.CameraBaseVo;
import com.relytosoft.mizar.media.camera.api.pojo.vo.SysDistrictVo;
import com.relytosoft.mizar.media.camera.services.service.CameraBaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author a3621
 */
@Api(tags = {"设备"})
@RestController
@RequestMapping("camera")
public class CameraController {

    @Resource
    private CameraBaseService cameraBaseService;

    @SaCheckPermission("camera:equipment:synchronous")
    @OpLog(msg = "获取三方平台同步", opType = OpTypeEnum.INSERT)
    @ApiOperation("同步")
    @GetMapping("sync/{type}")
    public ResponseResult<Void> sync(@PathVariable Integer type) {
        cameraBaseService.regionEncodeDevice(type);
        return ResponseResult.ok("数据已开始同步，稍后刷新查看");
    }

    @ApiOperation("同步数据至三方平台")
    @GetMapping("sync/btdl")
    public ResponseResult<Void> syncBtdl() {
        String msg = cameraBaseService.syncDeviceInfoToBtdlWithPassword();
        return ResponseResult.ok(msg);
    }

    @SaCheckPermission("camera:equipment:update")
    @OpLog(msg = "设备修改", opType = OpTypeEnum.UPDATE)
    @ApiOperation("修改")
    @PutMapping
    public ResponseResult<Void> update(@RequestBody CameraBaseDto cameraBaseDto) {
        CameraBase cameraBase = BeanCopyUtils.copy(cameraBaseDto, CameraBase.class);
        boolean b = cameraBaseService.updateCamera(cameraBase);
        if (b) {
            return ResponseResult.ok("修改成功");
        } else {
            return ResponseResult.fail("修改失败");
        }
    }

    @SaCheckPermission("camera:equipment:list")
    @OpLog(msg = "设备列表", opType = OpTypeEnum.SELECT)
    @ApiOperation("查询")
    @GetMapping("/list")
    public ResponseResult<Map<String, Object>> list(CameraBaseDto cameraBaseDto, PageQuery pageQuery) {
        IPage<CameraBaseVo> page = cameraBaseService.selectCameraPageList(cameraBaseDto, pageQuery);
        return ResponseResult.ok(CommonUtils.getPageData(page));
    }

    @SaCheckPermission("camera:equipment:getinfo")
    @OpLog(msg = "根据id查询", opType = OpTypeEnum.SELECT)
    @ApiOperation("详情")
    @GetMapping("/{id}")
    public ResponseResult<CameraBaseVo> findById(@PathVariable Integer id) {
        CameraBaseVo cameraBaseVo = cameraBaseService.findById(id);
        return ResponseResult.ok(cameraBaseVo);
    }


    @SaCheckPermission("camera:base:tree")
    @OpLog(msg = "树", opType = OpTypeEnum.OTHER)
    @ApiOperation("树")
    @GetMapping("/area")
    public ResponseResult<List<SysDistrictVo>> cameraArea() {
        List<SysDistrictVo> areaTree = cameraBaseService.findAreaTree();
        return ResponseResult.ok(areaTree);
    }


    @OpLog(msg = "查询设备下信息", opType = OpTypeEnum.SELECT)
    @ApiOperation("查询区域下设备信息")
    @GetMapping("/area/{id}")
    public ResponseResult<List<CameraBaseVo>> findByArea(@PathVariable Integer id) {
        return ResponseResult.ok(cameraBaseService.findByArea(id));
    }


    @OpLog(msg = "设备列表树", opType = OpTypeEnum.SELECT)
    @GetMapping(value = "/cameratreelist")
    @ApiOperation("设备列表树")
    public ResponseResult<List<Tree<Integer>>> selectVideoChannelTreeList(@ApiParam("区域ID")
                                                                          @RequestParam(required = false)
                                                                                  Integer districtId,
                                                                          @ApiParam("名称")
                                                                          @RequestParam(required = false)
                                                                                  String name) {
        return ResponseResult.ok(cameraBaseService.buildCameraDistrictListTree(districtId, name));
    }


    @GetMapping(value = "/test")
    public ResponseResult<String> test() {
        String deviceId = "00000224201310000013,00000224201310000027,00000224201310000028";
        String cameraNameOrCodeById = cameraBaseService.findCameraNameOrCodeById(deviceId, 1);
        return ResponseResult.ok(cameraNameOrCodeById, "成功获取数据");
    }
}
